﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassCommon
{
    public class PagingHelper
    {
        /// <summary>
        /// 获取翻页HTML
        /// </summary>
        /// <param name="pageCount">数据总条数</param>
        /// <param name="onePageCount">一页显示多少条</param>
        /// <param name="currentPage">当前页</param>
        /// <returns>返回翻页的HTML</returns>
        public static string getPaingHTML(int pageCount, int onePageCount, int currentPage)
        {

            /*
                

<div class="row">
					<div class="rfy col-lg-6 col-md-10 col-sm-12">
					<div class="fy">
						<ul>
							<li class="active2">首页</li>
							<li>上一页</li>
							<li>1</li>
							<li>2</li>
							<li>3</li>
							<li>下一页</li>
							<li>尾页</li>
						</ul>
					</div>
					</div>
				</div>
             */

            if (pageCount <= onePageCount) return string.Empty;



            // 一共有多少页
            int allPages = (pageCount % onePageCount == 0 ? pageCount / onePageCount : pageCount / onePageCount + 1);

            // 起始页索引
            int beginIndex = (currentPage - 4 <= 0 ? 0 : currentPage - 4);
            // 结束页索引
            int endIndex = (currentPage + 5 >= allPages ? allPages : currentPage + 5);

            StringBuilder pagingBuilder = new StringBuilder();
            pagingBuilder.AppendLine("<div class='row'>");
            pagingBuilder.AppendLine("<div class='rfy col-lg-6 col-md-10 col-sm-12'>");
            pagingBuilder.AppendLine("<div class='fy'>");
            pagingBuilder.AppendLine("<ul>");

            // 首页
            pagingBuilder.AppendLine(string.Format("<li><a href='?page={0}'>首页</a></li>", 0));


            // 上一页
            if (currentPage == 0)
            {
                pagingBuilder.AppendLine("<li><a href='javascript:void(0);'>上一页</a></li>");
            }
            else
            {
                pagingBuilder.AppendLine(string.Format("<li><a href='?page={0}'>上一页</a></li>", currentPage - 1));
            }

            for (int i = beginIndex; i < endIndex; i++)
            {
                if (currentPage == i)
                {
                    pagingBuilder.AppendLine(string.Format("<li><a class='active2' href='javascript:void(0)'>{0}</a></li>", i + 1));
                }
                else
                {
                    pagingBuilder.AppendLine(string.Format("<li><a href='?page={0}'>{1}</a></li>", i, i + 1));
                }
            }

            // 下一页
            if (currentPage == allPages - 1)
            {
                pagingBuilder.AppendLine(string.Format("<li><a href='javascript:void(0);'>下一页</a></li>"));
            }
            else
            {
                pagingBuilder.AppendLine(string.Format("<li><a href='?page={0}'>下一页</a></li>", currentPage + 1));
            }

            // 尾页
            pagingBuilder.AppendLine(string.Format("<li><a href='?page={0}'>尾页</a></li>", allPages - 1));


            pagingBuilder.AppendLine("</ul></div></div></div>");

            return pagingBuilder.ToString();
        }
    }
}
